package com.smarthospital.web.controller;

import com.smarthospital.common.Result.CodeMsg;
import com.smarthospital.common.Result.CommonException;
import com.smarthospital.common.Result.ResultDTO;
import com.smarthospital.contants.SHConst;
import com.smarthospital.entity.pojo.SHDictionary;
import com.smarthospital.entity.pojo.User;
import com.smarthospital.service.CommonService;
import com.smarthospital.service.FileService;
import com.smarthospital.web.inteceptor.LoginInterceptor;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * create by sym  2020/8/16 16:00.
 */
@RestController
@Slf4j
@RequestMapping("/common")
@Api(description = "公共接口 _短信 _文件",value = "common")
public class CommonController {

    @Autowired
    private CommonService commonService;
    @Autowired
    private FileService fileService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping("/optSend")
    @ApiOperation(value = "获取短信验证码",
                    notes = "param: sceneType -> 短信验证码场景类型\n"+
                             "0,提交健康卡\n" +
                            "1,预约体检)")
    public ResultDTO optSend(@RequestParam("sceneType") Integer sceneType,@RequestHeader("SH_TOKEN")String SH_TOKEN,
                            @RequestParam("phone") String  phone) throws Exception {
        log.info("optGet sceneType {}",sceneType);
        User user = LoginInterceptor.getUser();
        //增加防刷锁
        String key = String.format(SHConst.redis_key.optSend,user.getId(),phone,sceneType) ;
        if (redisTemplate.opsForValue().setIfAbsent(key,"1",2L,TimeUnit.SECONDS)) {
            throw new CommonException(CodeMsg.ERROR_OP_FAST);
        }
        commonService.optSend(sceneType,phone);
        return ResultDTO.sucess();
    }

    @PostMapping("/fileUpload")
    @ApiOperation(value = "文件上传",
            notes = "param: file _表单提交")
    public ResultDTO fileUpload(@RequestHeader("SH_TOKEN")String SH_TOKEN,
                                @RequestBody MultipartFile file) throws Exception {
//        User user = LoginInterceptor.getUser();
        //增加防刷锁
//        String key = String.format(SHConst.redis_key.fileUpload,user.getId()) ;
//        if (redisTemplate.opsForValue().setIfAbsent(key,"1",2L,TimeUnit.SECONDS)) {
//            throw new CommonException(CodeMsg.ERROR_OP_FAST);
//        }
        String url = fileService.fileUpload(file);
        return ResultDTO.sucess(url);
    }

    @GetMapping("/dictionaryMap")
    @ApiOperation(value = "字典接口",
            notes = "param: dictionary -> 字典键\n")
    public ResultDTO dictionaryMap(@RequestParam("dictionary") String dictionary,@RequestHeader("SH_TOKEN")String SH_TOKEN){
        log.info("dictionaryMap dictionary {}",dictionary);
        Map<String,List<Map>> map = commonService.dictionaryMap(dictionary);
        return ResultDTO.sucess(map);
    }

}
